package com.tianfeng.fenghuotoutiao.ui.activity.AppListActibity;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.tianfeng.fenghuotoutiao.R;

import java.util.List;


/**
 * Created by DELL on 2018/3/7.
 */

public class RedpackAdater extends RecyclerView.Adapter<RedpackAdater.ViewHolder> {
    private Context mContext;
    private List<Redpackmodel.DataBean> data;
    LayoutInflater layoutInflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public RedpackAdater(Context mContext, List<Redpackmodel.DataBean> data) {
        this.mContext = mContext;
        this.data = data;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_redpack, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, mOnItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        holder.tv_name.setText(data.get(position).getWcname());
        holder.tv_num.setText("当前活跃人"+data.get(position).getWcnum());
        holder.tv_leixin.setText(data.get(position).getWcleixing());



        Glide.with(mContext).load(data.get(position).getWcphoto()).asBitmap().into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = holder.iv_photo.getLayoutParams();
                para.height = height;
                para.width = ScreenUtils.getScreenWidth(mContext);
                holder.iv_photo.setImageBitmap(resource);
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnRecyclerViewItemClickListener mOnItemClickListener = null;
     ImageView iv_photo;
     TextView tv_name;
     TextView tv_num;
     TextView tv_leixin;


        public ViewHolder(View itemView, OnRecyclerViewItemClickListener mListener) {
            super(itemView);
            this.mOnItemClickListener = mListener;
            itemView.setOnClickListener(this);
            iv_photo=itemView.findViewById(R.id.iv_photo);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_num=itemView.findViewById(R.id.tv_num);
            tv_leixin=itemView.findViewById(R.id.tv_leixin);

        }

        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(v, getAdapterPosition());
        }
    }

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int data);

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
